<template>
    <div class="login box">
        <img
            src="https://best-extension.extfans.com/theme/wallpapers/71e12b80033209d235d33007457d7a7c/4e1a28d784f626a8e874d5ee15b488.webp"
            alt="">
        <div class="login">
            <div class="login-title">
                <img
                    src="../../static/image/logo.png"
                    alt="">
                <p>混元形意太极门给你最优质的鞭术体验!</p>
            </div>
            <div class="login_box">
                <div class="title">
                    <span @click="change2(this)" v-bind:class="{aa: show}">密码登录</span>
                    <span @click="change1(this)" v-bind:class="{aa: !show}">短信登录</span>
                </div>
                <div class="inp" v-if="show">
                    <input type="text" placeholder="用户名 / 手机号码 / 邮箱" class="user" v-model="username">
                    <input type="password" name="" class="pwd" placeholder="密码" v-model="password">
                    <div id="geetest1"></div>
                    <div class="rember">
                        <p>
                            <input type="checkbox" class="no" v-model="remember_me"/>
                            <span>记住密码</span>
                        </p>
                        <p>忘记密码</p>
                    </div>
                    <button class="login_btn btn btn-primary" @click="get_captcha">登录</button>
                    <p class="go_login">没有账号
                        <router-link to="/register">立即注册</router-link>
                    </p>
                </div>
                <div class="inp" v-else>
                    <input type="text" placeholder="手机号码" class="user" v-model="phone">
                    <input type="text" class="pwd" placeholder="短信验证码" v-model="code">
                    <button id="get_code" class="btn btn-primary" @click="get_code">{{ lastTimeContent }}</button>
                    <button class="login_btn" @click="plogin">登录</button>
                    <span class="go_login">没有账号
                    <router-link to="/register">立即注册</router-link>
                </span>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: "Login",
    data() {
        return {
            username: '',
            password: '',
            remember_me: false,
            show: true,
            phone: '',
            code: '',
            lastTimeContent: '发送验证码',
        }
    },
    methods: {
        change1() {
            this.show = false
        },
        change2() {
            this.show = true
        },
        get_captcha() {
            if (this.username && this.password) {
                this.$axios({
                    url: this.$settings.HOST + "user/captcha/?t=" + (new Date()).getTime(),
                    method: "get",
                    params: {
                        username: this.username,
                    },
                }).then(res => {
                    let data = JSON.parse(res.data);
                    initGeetest({
                        gt: data.gt,
                        challenge: data.challenge,
                        product: "popup",
                        offline: !data.success,
                        new_captcha: data.new_captcha
                    }, this.handlerPopup);
                }).catch(error => {
                    this.$message.error(
                        {
                            message: '网络错误',
                            type: "error",
                            duration: 1000,
                        }
                    )
                })
            } else {
                this.$message.error({
                    message: "请输入用户名和密码",
                    type: "fail",
                    duration: 1000,
                })
            }
        },
        handlerPopup(captchaObj) {
            let self = this;
            captchaObj.onSuccess(function () {
                let validate = captchaObj.getValidate();
                self.$axios({
                    url: self.$settings.HOST + "user/captcha/",
                    method: "post",
                    data: {
                        geetest_challenge: validate.geetest_challenge,
                        geetest_validate: validate.geetest_validate,
                        geetest_seccode: validate.geetest_seccode
                    },
                }).then(res => {
                    console.log(res);

                    self.user_login();
                }).catch(error => {
                    console.log(error);
                });
            });
            captchaObj.appendTo("#geetest1");
        },
        user_login() {
            this.$axios({
                url: this.$settings.HOST + 'user/login/',
                method: 'post',
                data: {
                    username: this.username,
                    password: this.password,
                }
            }).then(res => {
                let storage = ''
                if (this.remember_me) {
                    storage = localStorage

                } else {
                    localStorage.clear()
                    storage = sessionStorage
                }
                if (res.data) {
                    storage.token = res.data.token;
                    storage.user = res.data.user;
                    storage.user_id = res.data.user_id;
                    this.$message({
                        message: "登陆成功",
                        type: "success",
                        duration: 1000,
                    })
                    this.$router.push("/")
                }
            }).catch(error => {
                this.$message({
                    message: "用户名或密码错误",
                    type: "error",
                    duration: 1000,
                })
            })
        },
        get_code() {
            if (this.phone) {
                this.$axios({
                    url: this.$settings.HOST + 'user/send/',
                    method: 'get',
                    params: {
                        phone: this.phone
                    }
                }).then(res => {
                    this.send()
                }).catch(error => {
                    this.$message({
                        message: error.response.data.message,
                        type: 'error',
                        duration: 1000
                    })
                })
            } else {
                this.$message({
                    message: '手机号格式有误',
                    type: 'warning',
                    duration: 1000
                })
            }
        },
        send() {
            if (this.lastTimeContent === '发送验证码') {
                let lastTime = 59;
                let timer = setInterval(() => {
                    if (lastTime > 0) {
                        this.lastTimeContent = lastTime + 's';
                        lastTime--;
                    } else {
                        window.clearInterval(timer);
                        this.lastTimeContent = '发送验证码'
                    }
                }, 1000);
            } else {
                this.$message({
                    message: '请不要频繁点击哦',
                    type: 'warning',
                    duration: 1000,
                });
            }
        },
        plogin() {
            if (this.phone && this.code) {
                this.$axios({
                    url: this.$settings.HOST + 'user/plogin/',
                    method: 'post',
                    data: {
                        phone: this.phone,
                        code: this.code
                    }
                }).then(res => {
                    localStorage.clear()
                    sessionStorage.token = res.data.token
                    sessionStorage.user = res.data.user.username
                    sessionStorage.user_id = res.data.user.id
                    this.$message({
                        message: "登陆成功",
                        type: "success",
                        duration: 1000,
                    })
                    this.$router.push("/")
                }).catch(error => {
                    this.$message({
                        message: error.response.data,
                        type: "error",
                        duration: 1000,
                    })
                })
            }
        },
    },
}
</script>

<style scoped>
.box {
    width: 100%;
    height: 100%;
    position: relative;
    overflow: hidden;
}

.box img {
    width: 100%;
    min-height: 100%;
}

.box .login {
    position: absolute;
    width: 500px;
    height: 400px;
    top: 0;
    left: 0;
    margin: auto;
    right: 0;
    bottom: 0;
    top: -338px;
}

.login .login-title {
    width: 100%;
    text-align: center;
}

.login-title img {
    width: 190px;
    height: auto;
}

.login-title p {
    font-family: PingFangSC-Regular;
    font-size: 18px;
    color: #fff;
    letter-spacing: .29px;
    padding-top: 10px;
    padding-bottom: 50px;
}

.login_box {
    width: 400px;
    height: auto;
    background: #fff;
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, .5);
    border-radius: 4px;
    margin: 0 auto;
    padding-bottom: 40px;
}

.login_box .title {
    font-size: 20px;
    color: #9b9b9b;
    letter-spacing: .32px;
    border-bottom: 1px solid #e6e6e6;
    display: flex;
    justify-content: space-around;
    padding: 50px 60px 0 60px;
    margin-bottom: 20px;
    cursor: pointer;
}

.aa {
    color: #4a4a4a;
    border-bottom: 2px solid #84cc39;
}

.inp {
    width: 350px;
    margin: 0 auto;
}

.inp input {
    border: 0;
    outline: 0;
    width: 100%;
    height: 45px;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    text-indent: 20px;
    font-size: 14px;
    background: #fff !important;
}

.inp input.user {
    margin-bottom: 16px;
}

.inp .rember {
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: relative;
    margin-top: 10px;
}

.inp .rember p:first-of-type {
    font-size: 12px;
    color: #4a4a4a;
    letter-spacing: .19px;
    margin-left: 22px;
    display: -ms-flexbox;
    display: flex;
    -ms-flex-align: center;
    align-items: center;
    /*position: relative;*/
}

.inp .rember p:nth-of-type(2) {
    font-size: 14px;
    color: #9b9b9b;
    letter-spacing: .19px;
    cursor: pointer;
}

.inp .rember input {
    outline: 0;
    width: 30px;
    height: 45px;
    border-radius: 4px;
    border: 1px solid #d9d9d9;
    text-indent: 20px;
    font-size: 14px;
    background: #fff !important;
}

.inp .rember p span {
    display: inline-block;
    font-size: 12px;
    width: 100px;
    /*position: absolute;*/
    /*left: 20px;*/

}

#geetest {
    margin-top: 20px;
}

.login_btn {
    width: 100%;
    height: 45px;
    background: #84cc39;
    border-radius: 5px;
    font-size: 16px;
    color: #fff;
    letter-spacing: .26px;
    margin-top: 30px;
}

.inp .go_login {
    text-align: center;
    font-size: 14px;
    color: #9b9b9b;
    letter-spacing: .26px;
    padding-top: 20px;
}

.inp .go_login span {
    color: #84cc39;
    cursor: pointer;
}
</style>
